// 小说详情处理
const cheerio = require('cheerio');
const abc_detail = (html, source, baseUrl) => {
	let $ = $1(html);
	let muluList = [];
	// 目录
	$('.listmain dl dt:nth-of-type(2)')
		.nextAll('dd')
		.each((i, item) => {
			muluList.push({
				chapterName: $(item).find('a').text(),
				chapterIndex: i + 1,
				chapterId: $(item).find('a').attr('href').split('/')[2].split('.')[0],
			});
		});
	let time = $('.small .last:eq(0)')
		.text()
		.split('：')[1]
		.split(' ')[0]
		.split('-');
	let updateTime = time[1] + '月' + time[2] + '日';
	// 推荐
	let info = {
		avatar: baseUrl + $('.cover img').attr('src'),
		bookName: $('.info h2').text(),
		author: $('.small span:eq(0)').text().split('：')[1],
		classify: $('.small span:eq(1)').text().split('：')[1],
		bookStatus: $('.small span:eq(2)').text().split('：')[1],
		updateTime,
		newChapterName: $('.small .last:eq(1) a').text(),
		intro: $('.intro')
			.text()
			.split('简介：')[1]
			.split('作者：')[0]
			.split('无弹窗')[0],
		source,
		muluList,
	};
	return info;
};

const qianbi_detail = (html, source) => {
	let $ = $1(html);
	let muluList = [];
	$('#chapterList li').each((i, item) => {
		muluList.push({
			chapterId: _chapterId($(item).find('a')),
			chapterName: $(item).find('a').text(),
			chapterIndex: i + 1,
		});
	});
	return {
		avatar: $('#bookimg img').attr('src'),
		bookName: $('.d_title').text().trim(),
		author: $('#count li:first-child a').text(),
		classify: $('#count li:nth-child(2) span').text(),
		bookStatus: $('#count li:nth-child(3) span').text(),
		uptime: $('#count #uptime span').text(),
		intro: $('#bookintro p').text(),
		newChapterName: $('.chaw li:nth-child(1) a').text(),
		source,
		muluList,
	};
};
function $1(html) {
	return cheerio.load(html);
}

function _chapterId(dom) {
	return dom.attr('href').split('/')[3].split('.')[0];
}
module.exports = {
	abc_detail,
	qianbi_detail,
};
